﻿@using LanDBom.Models;
@using LanDBom.Resources
@{
    ViewBag.Title = "Add_log";
}

@section styles{
    <style>
        #boun {
            height: 200px;
            width: 1200px;
            overflow: scroll
        }
        #boun2 {
            width: 1200px;
        }
        #boun3 {
            width: 1200px;
        }
        #RZ_xznr {
            width: 500px;
        }
        fieldset div {
            margin-bottom: 5px;
            margin-left:5px;
        }
        #tab_wj td{
            font-size:10px;
        }
    </style>
}
<form>
    <div id="WT_dh_mb" style="display:none;position:fixed;background-color:white;top:200px;width:660px;left:300px;z-index:1000;border:1px black solid;">
        <div id="WJ_ID" style="display:none"></div>
        <div id="WJ_tjr" style="display:none"></div>
        <table>
            <tr>
                <td>@Resource.String548：</td>
                <td id="WT_dhr"></td>
            </tr>
            <tr>
                <td>@Resource.String549</td>
                <td><textarea id="WT_dhly"></textarea></td>
            </tr>
            <tr>
                <td id="WT_qdbh">@Resource.String550</td>
                <td id="WT_qxbh">@Resource.String551</td>
            </tr>
        </table>
    </div>
    <div id="WT_dh_mb2" style="display:none;position:fixed;background-color:white;top:200px;width:660px;left:300px;z-index:1000;border:1px black solid;">
        <table>
            <tr>
                <td>@Resource.String552：</td>
                <td id="WT_dhr2"></td>
            </tr>
            <tr>
                <td>@Resource.String553</td>
                <td><textarea id="WT_dhly2"></textarea></td>
            </tr>
            <tr>
                <td id="WT_qxbh2">@Resource.String555</td>
            </tr>
        </table>
    </div>
    <div id="WT_dh_mb3" style="display:none;position:fixed;background-color:white;top:200px;width:660px;left:300px;z-index:1000;border:1px black solid;">
        <table>
            <tr>
                <td>@Resource.String554</td>
                <td><textarea id="WT_sjbz"></textarea></td>
            </tr>
            <tr>
                <td id="WT_GB">@Resource.String555</td>
            </tr>
        </table>
    </div>

    <div id="RZ_D_bian">@(Session["names_RZ"])</div>
    <div id="RZ_D_xmfqr" style="display:none">@(Session["name_xmfqr"])</div>

    <div class="row">
        <div id="RZ_fhck" class="col-2">@Resource.String556</div>
        <div id="RZ_WC" class="col-2">@Resource.String557</div>
    </div>
    <div></div>

    <div id="bigbox1">
        <fieldset class="boun" id="boun">
            <legend class="len">@Resource.String558</legend>
            <div id="RZ_gwzr">
                @if (Session["names_RZ"] != null)
                {
                    foreach (var item in ViewBag.Listlog as List<LanDBom.Models.BeiZhu>)
                    {
                        <div>
                            <div>
                                <span style="color:red">@item.B_BR</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span>@item.B_Date</span>
                            </div>
                            <div style="border-bottom:1px solid #808080">
                                @item.B_bei
                            </div>
                        </div>
                    }
                }
            </div>
        </fieldset>
    </div>
    <div>
        <fieldset class="boun" id="boun2">
            <legend class="len">@Resource.String560</legend>
            <div class="row">
                <div class="col-2">@Resource.String561</div>
                <div class="col-3"><input type="text" id="RZ_bjr" /></div>
                <div class="col-2">@Resource.String562</div>
                <div class="col-3" id="RZ_xzsj"></div>
            </div>
            <div class="row">
                <div class="col-2">@Resource.String563</div>
                <div class="col-8"><textarea id="RZ_xznr" cols="30"></textarea></div>
            </div>
            <div class="row">
                <div id="RZ_tjrz" class="col-2">@Resource.String564</div>
            </div>
        </fieldset>
    </div>
    <div class="row">
        <div class="col-3" id="CKSJBZ">@Resource.String559</div>
        <div class="col-3" id="WCSJ">完成设计</div>
    </div>
    <div>
        <fieldset class="boun" id="boun3">
            <legend class="len">@Resource.String565</legend>
            <table style="width:100%;text-align:center" id="tab_wj">
                <thead>
                    <tr>
                        <td>@Resource.String566</td>
                        <td>@Resource.String567</td>
                        <td>@Resource.String568</td>
                        <td>@Resource.String569</td>
                        <td>@Resource.String570</td>
                        <td>@Resource.String571</td>
                        <td>对应BOM</td>
                        <td>@Resource.String572</td>
                        <td>审核</td>
                        <td>上传</td>
                    </tr>
                </thead>
                <tbody id="logfuj">
                </tbody>
                <tfoot>
                    <tr>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td>
                            <select id="sjt_BOM">
                            </select>
                        </td>
                        <td><input type="file" id="log_XZFJ" value="@Resource.String573"></td>
                        <td></td>
                        <td><a href="#" id="log_SCFJ">@Resource.String574</a></td>
                    </tr>
                </tfoot>
            </table>
        </fieldset>
    </div>
</form>

@section scripts
{
    <script src="~/Scripts/jquery-3.4.1.min.js"></script>
    <script src="~/Scripts/bootstrap.min.js"></script>
    <script>
        var RZ_bian = $("#RZ_D_bian").text()
        /*function openFileDialog() {
            var openFileDialog = document.getElementById("<%= openFileDialog1.ClientID %>");
            openFileDialog.click();
        }  */
        $(function () {

            //获取当前时间的方法
            randomGeneration()
            Load_design_table()
            Get_BOM()
            /*Get_BZ()*/
            function randomGeneration() {
                var now = new Date($.now());
                var year = (now.getFullYear()).toString();
                var month = (now.getMonth() + 1).toString();
                var date = (now.getDate()).toString();
                var hours = (now.getHours()).toString()
                var minutes = (now.getMinutes()).toString()

                //组合当前日期
                var sqrq = year + "/" + month + "/" + date + " " + hours + ":" + minutes

                //写入当年月日
                $("#RZ_xzsj").text(sqrq)
            }
            //完成上传任务
            $(document).on("click", "#WCSJ", function () {
                var BZJG = [];
                BZJG.push({
                    SJR_id: $("#Login_name").text(),
                    SJ_Bian: $("#RZ_D_bian").text(),
                    SJ_ZT: "完成"
                })
                $.ajax({
                    url: "/Contracts/chaneSJBZ",
                    type: 'POST',
                    data: JSON.stringify(BZJG),
                    contentType: "application/json;charset=utf-8",
                    dataType: "json",
                    success: function (text) {
                        alert(text)
                    },
                    error: function (text) {
                        alert("系统错误" + text);
                    }
                })
            })
            //日志写入数据库
            $(document).on("click", "#RZ_tjrz", function () {
                var rzxx = {
                    B_Bian: RZ_bian,
                    B_BR: $('#RZ_bjr').val(),
                    B_Date: $('#RZ_xzsj').text(),
                    B_bei: $('#RZ_xznr').val(),

                };
                console.log(rzxx)
                $.ajax({
                    url: "/Contracts/AddLogs",
                    type: "POST",
                    data: rzxx,
                    success: function (data) {
                        location.reload(true)
                        alert("tjcg")
                    },
                    error: function (data) {
                        alert("erroer");
                    },
                })
            })
            //搜索当前申请单下的BOM
            function Get_BOM() {
                $.ajax({
                    url: '/Contracts/Add_log',
                    type: 'GET',
                    data: { "RZ_bian": RZ_bian, "state": 6 },
                    success: function (bz) {
                        newBMO(bz);
                    },
                    error: function () {
                        alert('Request Error');
                    }
                })
            }
            function newBMO(bz) {
                $('#sjt_BOM div').remove();
                $('#sjt_BOM').append($(`<option value="0">无</option>`));
                $.each(bz, function (i, item) {
                    var $bz = $(`<option value="${item.BOM2_id}">${item.BOM2_wuLiu}</option>`);
                    $('#sjt_BOM').append($bz);
                })
            }
            //搜索当前申请单下的所有日志信息
            function Get_BZ() {
                $.ajax({
                    url: '/Contracts/Inspect_Contracts',
                    type: 'GET',
                    data: { "RZ_bian": RZ_bian, "state": 1 },
                    success: function (bz) {
                        newbz(bz);
                    },
                    error: function () {
                        alert('Request Error');
                    }
                })
            }
            //打印日志
            function newbz(bz) {
                $('#RZ_gwzr div').remove();
                $.each(zc, function (i, item) {
                    var $bz = $(`
                        <div>
                            <div>
                                <span>${item.B_BR}</span><span>${item.B_Date}</span>
                            </div>
                            <div>
                                ${item.B_bei}
                            </div>
                        </div>
                    `);
                    $('#RZ_gwzr').append($bz);
                })
            }
            //显示审核
            function show_sh() {
                if ($("#Login_sf").text() == "设计主管") {
                    $(".Log_sh").css({"display":"block"})
                }
            }
            //加载设计表
            function Load_design_table() {
                $.ajax({
                    url: '/Contracts/Add_log',
                    type: 'POST',
                    data: { "state": 3, "RZ_bian": RZ_bian },
                    success: function (path) {
                        createBom(path)
                        show_sh()
                    },
                    error: function () {
                        alert("系统错误!");
                    }
                })
                function createBom(fgggh) {
                    $("#logfuj tr").remove();
                    $.each(fgggh, function (i, item) {
                        var x = item.De_dizhi
                        if (item.DE_tuiLei == "驳回") {
                            var $tr = (`
                            <tr class="log_tr">
                                <td style="display:none">${item.DE_id}</td>
                                <td>${i + 1}</td>
                                <td>${item.De_dizhi}</td>
                                <td>${item.De_shijian}</td>
                                <td>${item.De_ren}</td>
                                <td>
                                    <input type="number" readonly="readonly" value="${item.DE_Slong}" style="width:60px;height:30px"/>
                                    <span class="Log_sh" id="change_time">@Resource.String575</span>
                                </td>
                                <td class="WT_SHZT">${item.DE_tuiLei}<a href="#" id="WT_CKLY">@Resource.String576</a></td>
                                <td><a href="#" id="log_SC">删除</a>/<a href="/Contracts/Download_file?path=${x}" id="log_SZ">@Resource.String568</a></td>
                                <td class="Log_sh" style="display:none"><a href="#" id="WT_pass">@Resource.String584</a>/<a href="#" id="log_back">@Resource.String579</a></td>
                            </tr>`);
                            $("#logfuj").append($tr);
                        }
                        else {
                            var $tr = (`
                            <tr class="log_tr">
                                <td style="display:none">${item.DE_id}</td>
                                <td>${i + 1}</td>
                                <td>${item.De_dizhi}</td>
                                <td>${item.De_shijian}</td>
                                <td>${item.De_ren}</td>
                                <td>
                                    <input type="number"  value="${item.DE_Slong}" style="width:60px;height:30px"/>
                                    <span class="Log_sh" id="change_time">@Resource.String575</span>
                                </td>
                                <td class="WT_SHZT">${item.DE_tuiLei}</td>
                                <td class="">${item.DE_tuiLei}</td>
                                <td><a href="#" id="log_SC">@Resource.String581</a>/<a href="/Contracts/Download_file?path=${x}" id="log_SZ">@Resource.String582</a></td>
                                <td class="Log_sh" style="display:none"><a href="#" id="WT_pass">@Resource.String584</a>/<a href="#" id="log_back">@Resource.String583</a></td>
                            </tr>`);
                            $("#logfuj").append($tr);
                        }

                    })
                }
            }

            //上传附件(文件路径)
            function Upload_attachment() {
                var fileUpload = $('#log_XZFJ')[0];
                if (fileUpload.files.length > 0) {
                    var fileData = new FormData();
                    fileData.append('file', fileUpload.files[0]);
                    console.log(fileData)
                    var userInput = prompt("请输入设计用时：", "")
                    if (userInput != null) {
                        var now = new Date($.now());
                        var year = (now.getFullYear()).toString();
                        var month = (now.getMonth() + 1).toString();
                        var date = (now.getDate()).toString();
                        var hours = (now.getHours()).toString()
                        var minutes = (now.getMinutes()).toString()
                        var name = $("#Login_N").text()
                        //组合当前日期
                        var sqrq = year + "/" + month + "/" + date + " " + hours + ":" + minutes
                        var xh = $(".log_tr").length
                        var lujing = null
                        $.ajax({
                            url: '/Contracts/AddNdardo',
                            type: 'POST',
                            contentType: false, // 不设置内容类型
                            processData: false, // 不处理数据
                            data: fileData,
                            success: function (response) {
                                var BZJG = [];
                                BZJG.push({
                                    DE_dizhi: response,
                                    DE_shijian: sqrq,
                                    DE_shen: RZ_bian,
                                    De_ren: name,
                                    DE_Slong: userInput,
                                    DE_tuiLei: "待审核",
                                    bom_id: $("#sjt_BOM").val()
                                })
                                $.ajax({
                                    url: '/Contracts/adddes',
                                    type: 'POST',
                                    contentType: "application/json;charset=utf-8", // 不设置内容类型
                                    dataType: "json",
                                    processData: false, // 不处理数据
                                    data: JSON.stringify(BZJG), fileData,
                                    success: function (response) {
                                        console.log(response);
                                        Load_design_table()
                                        Agent_processing()
                                    },
                                    error: function (jqXHR, textStatus, errorMessage) {
                                        alert('AJAX请求失败：' + errorMessage);
                                    }
                                });
                            },
                            error: function (jqXHR, textStatus, errorMessage) {
                                alert('AJAX请求失败：' + errorMessage);
                            }
                        });


                    }

                }
                else {
                    alert('请选择要上传的文件！');
                }

            }
            //增加代办事项的方法
            function Agent_processing() {
                var name = $("#Login_N").text()
                var BZJG = [];
                BZJG.push({
                    Fr_fl: "设计委托",//待处理模块
                    De_tiji: "审核",//待处理内容
                    Cl_juli:"设计主管" ,//接收人
                    Nd_jiage: name,//发起人
                    Fr_zhuang:"待处理"//装态

                })
                console.log("共" + BZJG.length + "数据");
                console.log(BZJG);
                $.ajax({
                    url: "/Contracts/Addfr",
                    type: 'POST',
                    data: JSON.stringify(BZJG),
                    contentType: "application/json;charset=utf-8",
                    dataType: "json",
                    success: function (text) {
                        alert(text)
                    },
                    error: function (text) {
                        alert("系统错误" + text);
                    }
                })
            }
            //删除上传的文件
            function Delete_file(e) {
                var x = e.parent().parent().children().eq(0).text()
                $.ajax({
                    url: '/Contracts/Add_log',
                    type: 'POST',
                    data: { "state": 2, "number": x },
                    success: function (path) {
                        alert(path)
                        Load_design_table()
                    },
                    error: function () {
                        alert("系统错误!");
                    }
                })
            }
            //通过设计图的方法
            

            function chanezhuang() {
                $.ajax({
                    url: '/Contracts/Add_log',
                    type: 'GET',
                    data: { "RZ_bian": id, "state": 4 },
                    success: function (zc) {
                        if (zc == "全部提交") {
                            complete_plan()
                        }
                    },
                    error: function () {
                        alert('Request Error');
                    }
                })
            }

            //驳回设计图的方法
            function Rejected_plan() {
                var de_id = $("#WJ_ID").text()
                var bhly = $("#WT_dhly").val()
                var shr = $("#WT_dhr").text()
                if (bhly == "") {
                    alert("请输入驳回理由")
                }
                else {
                    var BZJG = [];
                    BZJG.push({
                        DE_id: de_id,
                        DE_JDR: shr,
                        DE_wenjain: bhly,
                        DE_tuiLei: "驳回"

                    })
                    console.log("共" + BZJG.length + "数据");
                    console.log(BZJG);
                    $.ajax({
                        url: "/Contracts/Modify_INF_zhuang",
                        type: 'POST',
                        data: JSON.stringify(BZJG),
                        contentType: "application/json;charset=utf-8",
                        dataType: "json",
                        success: function (text) {
                            alert(text)
                            var mbr = $("#WJ_tjr").text()
                            var name = $("#Login_N").text()
                            var BZJG = [];
                            BZJG.push({
                                Fr_fl: "设计委托",//待处理模块
                                De_tiji: "设计图被驳回",//待处理内容
                                Cl_juli: mbr,//接收人
                                Nd_jiage: name,//发起人
                                Fr_zhuang: "待处理"//装态

                            })
                            console.log("共" + BZJG.length + "数据");
                            console.log(BZJG);
                            $.ajax({
                                url: "/Contracts/Addfr",
                                type: 'POST',
                                data: JSON.stringify(BZJG),
                                contentType: "application/json;charset=utf-8",
                                dataType: "json",
                                success: function (text) {
                                    $("#WT_dh_mb").css({ "display": "none" })
                                },
                                error: function (text) {
                                    alert("系统错误" + text);
                                }
                            })
                        },
                        error: function (text) {
                            alert("系统错误" + text);
                        }
                    })
                }
            }
            //修改设计委托状态
            function complete_plan() {
                var index = 0
                $(".WT_SHZT").each(function (i, item) {
                    if ($(item).text() != "通过") {
                        index+=1
                    }
                })
                if (index == 0) {
                    var RZ_bian = $("#RZ_D_bian").text()
                    var BZJG = [];
                    BZJG.push({
                        D_Ton: RZ_bian,
                        D_zhuang: "4"
                    })
                    console.log("共" + BZJG.length + "数据");
                    console.log(BZJG);
                    $.ajax({
                        url: "/Contracts/Modify_des_zhuang",
                        type: 'POST',
                        data: JSON.stringify(BZJG),
                        contentType: "application/json;charset=utf-8",
                        dataType: "json",
                        success: function (text) {
                            alert(text)


                            var name = $("#Login_N").text()
                            var jsr = $("RZ_D_xmfqr").text()
                            var BZJG = [];
                            BZJG.push({
                                Fr_fl: "设计委托",//待处理模块
                                De_tiji: "设计已完成",//待处理内容
                                Cl_juli: jsr,//接收人
                                Nd_jiage: name,//发起人
                                Fr_zhuang: "待处理"//装态

                            })
                            console.log("共" + BZJG.length + "数据");
                            console.log(BZJG);
                            $.ajax({
                                url: "/Contracts/Addfr",
                                type: 'POST',
                                data: JSON.stringify(BZJG),
                                contentType: "application/json;charset=utf-8",
                                dataType: "json",
                                success: function (text) {
                                },
                                error: function (text) {
                                    alert("系统错误" + text);
                                }
                            })
                        },
                        error: function (text) {
                            alert("系统错误" + text);
                        }

                    })
                }
            }
            function find_liy(id) {
                $.ajax({
                    url: '/Contracts/Add_log',
                    type: 'GET',
                    data: { "number": id, "state": 4 },
                    success: function (zc) {
                        $.each(zc, function (i, obj) {
                            console.log(obj.D_dhly)
                            $("#WT_dhly2").val(obj.DE_wenjain)
                            $("#WT_dhr2").text(obj.DE_JDR)
                        })
                    },
                    error: function () {
                        alert('Request Error');
                    }
                })
            }

            //通过设计图的方法
            function change_time(e) {
                DE_id = e.parent().parent().children().first().text()//设计图id
                DE_Slong = e.prev().val()
                var BZJG = [];
                BZJG.push({
                    DE_id: DE_id,
                    DE_Slong: DE_Slong

                })
                console.log("共" + BZJG.length + "数据");
                console.log(BZJG);
                $.ajax({
                    url: "/Contracts/Modify_des_Slong",
                    type: 'POST',
                    data: JSON.stringify(BZJG),
                    contentType: "application/json;charset=utf-8",
                    dataType: "json",
                    success: function (text) {
                        alert(text)
                    },
                    error: function (text) {
                        alert("系统错误" + text);
                    }
                })
            }
            //搜索设计备注根据申请单号和用户id
            function find_SZBZ() {
                $.ajax({
                    url: '/Contracts/Add_log',
                    type: 'GET',
                    data: { "number": $("#Login_name").text() * 1, "RZ_bian": RZ_bian, "state": 5 },
                    success: function (zc) {
                        $.each(zc, function (i, obj) {
                            $("#WT_sjbz").val(obj.SJ_BZ)
                        })
                    },
                    error: function () {
                        alert('Request Error');
                    }
                })
            }

            function Through_drawing(e) {
                DE_id = e.parent().parent().children().first().text()
                var num = $("#Login_N").text()
                var BZJG = [];
                BZJG.push({
                    DE_id: DE_id,
                    DE_JDR: num,
                    DE_tuiLei: "通过"

                })
                console.log("共" + BZJG.length + "数据");
                console.log(BZJG);
                $.ajax({
                    url: "/Contracts/Modify_INF_zhuang2",
                    type: 'POST',
                    data: JSON.stringify(BZJG),
                    contentType: "application/json;charset=utf-8",
                    dataType: "json",
                    success: function (text) {
                        alert(text)
                        chanezhuang()
                    },
                    error: function (text) {
                        alert("系统错误" + text);
                    }
                })
            }
            //关闭设计备注
            $(document).on("click", "#WT_GB", function () {
                $("WT_dh_mb3").css({ "display": "none" })
            })
            //点击查看设计备注
            $(document).on("click", "#CKSJBZ", function () {
                $("WT_dh_mb3").css({ "display": "block" })
                find_SZBZ()
            })
            //点击修改时间
            $(document).on("click", "#change_time", function (e) {
                change_time($(e.target))
            })
            //点击关闭驳回面板
            $(document).on("click", "#WT_qxbh2", function (e) {
                $("#WT_dh_mb2").css({ "display": "none" })
                $("#WT_dhly2").val("")
                $("#WT_dhr2").text("")
            })
            //点击查看设计主管的打回理由
            $(document).on("click", "#WT_CKLY", function (e) {
                id = $(e.target).parent().parent().children().first().text()
                find_liy(id)
                $("#WT_dh_mb2").css({ "display": "block" })
            })
            //点击完成设计委托
            $(document).on("click", "#RZ_WC", function (e) {
                complete_plan()
                window.location.href = "/Contracts/Inspect_Contracts"
            })
            //点击通过设计图
            $(document).on("click", "#WT_pass", function (e) {
                Through_drawing($(e.target))
                Load_design_table()
            })
            //点击驳回设计图
            $(document).on("click", "#WT_qdbh", function () {
                Rejected_plan()
                $("#WT_dh_mb").css({ "display": "none" })
            })
            //点击打开驳回面板
            $(document).on("click", "#log_back", function (e) {
                var id = $(e.target).parent().parent().children().first().text()
                var name = $(e.target).parent().parent().children().eq(4).text()
                $("#WJ_ID").text(id)
                $("#WT_dh_mb").css({ "display": "block" })
                var num = $("#Login_N").text()
                $("#WT_dhr").text(num)
                $("#WJ_tjr").text(name)
            })
            //点击关闭驳回面板
            $(document).on("click", "#WT_qxbh", function (e) {
                $("#WT_dh_mb").css({ "display": "none" })
                $("#WT_dhly").val("")
                $("#WT_dhr").text("")
                $("#WJ_ID").text("")
                $("#WJ_tjr").text("")
            })
            //点击下载附件
            $(document).on("click", "#log_SZ", function (e) {
                Download_file($(e.target))
            })
            //点击删除附件
            $(document).on("click", "#log_SC", function (e) {
                Delete_file($(e.target))
            })
            //提交设计附件（文件路径）
            $(document).on("click", "#log_SCFJ", function (e) {
                Upload_attachment($(e.target))
            })
            //返回查看页面
            $(document).on("click", "#RZ_fhck", function () {
                window.location.href = "/Contracts/Inspect_Contracts"
            })
        })

    </script>
}